#include "free_tile.h"

namespace freetile
{
    void explode(DELAY_PARAMS &params)
    {
        // Get map
        CMap *pMap(CMapContainer::getInstance()->getCurrentMap());

        int *npTextureFrameIndex(GET_POINTER_FROM_PARAM(params, 0, int));
        CFreeTile *pTile(GET_POINTER_FROM_PARAM(params, 1, CFreeTile));

        // Get the height, width, depth of this tile
        int nHeight(pTile->getHeight());
        int nWidth(pTile->getWidth());
        int nDepth(pTile->getDepth());

        pTile->setVisible(true);

        if (++(*npTextureFrameIndex) >= 4)
        {
            pTile->setVisible(false);
            pTile->stopRepeat();

            *npTextureFrameIndex = 0;

            pTile->setDestroyed(false);

            //delete npTextureFrameIndex;
            npTextureFrameIndex = nullptr;
        }
        else
        {
            // Change texture
            stringstream sstream;
            sstream << "explosion"
                    << *npTextureFrameIndex;

            CTexture *pTexture(
                CTextureContainer::getInstance()->getTexture(sstream.str()));
            if (pTexture == nullptr)
            {
                pTexture = CTextureContainer::getInstance()->addTexture(sstream.str(),
                                            sstream.str(), 
                                            CDeviceManager::getInstance()->getDevice());
            }
            pTile->getDisplayable()->setTexture(pTexture, 0);
            pTile->getDisplayable()->setTexture(pTexture, 1);
            pTile->getDisplayable()->setTexture(pTexture, 2);
            pTile->getDisplayable()->setTexture(pTexture, 3);
            pTile->getDisplayable()->setTexture(pTexture, 4);
            pTile->getDisplayable()->setTexture(pTexture, 5);
        
        }
    }
};

void CFreeTile::tick()
{
    myRepeat.tick();
    CSolidTile::tick();
}


void CFreeTile::stopRepeat()
{
    myRepeat.stop();
}

CFreeTile::CFreeTile(short int nHeight, short int nWidth, short int nDepth)
    : CSolidTile(ENT_TILE_FREE, "textures/tiles/notexture.tga", nHeight, nWidth, nDepth),
      myRepeat(100.0f, &freetile::explode, 4)
{
    setVisible(false);
    setWalkable(true);
    setDestructible(false);
}

void CFreeTile::destroy()
{
    if (! myRepeat.isRunning() && ! isBeingDestroyed())
    {
        setDestroyed();

        // Get map
        CMap *pMap(CMapContainer::getInstance()->getCurrentMap());

        myRepeat.clearParams();

        myRepeat.registerParam(new int(0), utils::TYPE_POINTER, sizeof(int));
        //myRepeat.registerParam(0, utils::TYPE_SLONG, sizeof(int));
        myRepeat.registerParam(this, utils::TYPE_POINTER, sizeof(*this));

        // Get the height, width, depth of this tile
        //int nHeight(getHeight());
        //int nWidth(getWidth());
        //int nDepth(getDepth());

        // Create reference to current tile
        //CTile *pCurrentTile (pMap->getTile(nHeight, nWidth, nDepth));

        // Create and start the repeat
        myRepeat.start();
    }
}